{% extends 'layout.html' %}

{% block title %}{{ dbName }}{% endblock %}

{% block breadcrumb %}
<li>
  <a href="">Home</a>
  <span class="divider">/</span>
</li>
<li class="active">
  {{ dbName }}
</li>
{% endblock %}


{% block content %}
<h2>Create Collection</h2>
<form class="well form-inline" method="POST">
    <div class="input-prepend">
      <span class="add-on">{{ dbName }} . </span>
      <input class="input-medium" type="text" id="collection" name="collection" placeholder="Collection Name" title="Collection Name">
    </div>
  <button type="submit" class="btn btn-primary">
  <i class="icon-folder-open icon-white"></i>
  Create collection
  </button>
</form>

<script>
  $( document ).ready( function() {
    $( '#collection' ).popover({ content: "Collection names must begin with a letter or underscore, and can contain only letters, numbers, underscores and dots." });
    
    $( '.deleteButton' ).popover({ placement: "left", title: "Warning", content: "Are you sure you want to delete this collection? All documents will be deleted." });
    
    $( '.deleteButton' ).on( 'click', function( event ) {
      event.preventDefault();
      var target = $( this );
      var parentForm = $( '#' + target.attr( 'childof' ) );
      
      $( '#confirmation-input' ).attr( 'shouldbe', target.attr( 'collection-name' ) );
      $( '#modal-collection-name' ).text( target.attr( 'collection-name' ) );
      $( '#confirm-deletion' ).modal({ backdrop: 'static', keyboard: false })
        .one( 'click', '#delete', function() {  
          var input = $( '#confirmation-input' );
          if ( input.val().toLowerCase() === input.attr( 'shouldbe' ).toLowerCase() ) {
            parentForm.trigger( 'submit' );  
          } else {
            // wrong collection name
          }
    });
   });
 });
</script>


<h2>Collections</h2>
<table class="table table-bordered table-striped table-condensed">
  {% for c in colls %}
  <tr>
    <td class="span2">
      <a href="db/{{ dbName }}/{{ c }}" class="btn btn-success span2">
        <i class="icon-list-alt icon-white"></i> View
      </a>
    </td>
	<td class="span2">
	     <a href="db/{{ dbName }}/export/{{ c }}" class="btn btn-warning span2">
	      <i class="icon-download icon-white"></i> Export
	    </a>
	</td>
    <td><h3><a href="db/{{ dbName }}/{{ c }}">{{ c }}</a></h3></td>
    <td class="span2">
      <form method="POST" id="db-{{ dbName }}-{{ c }}" action="db/{{ dbName }}/{{ c }}" style="margin: 0px;">
        <input type="hidden" name="_method" value="delete">
        <button type="submit" class="hidden"></button>
      </form>
      <button class="btn btn-danger span2 deleteButton" collection-name="{{ c }}" childof="db-{{ dbName }}-{{ c }}">
        <i class="icon-trash icon-white"></i>
        Delete
      </button>
    </td>
  </tr>
  {% endfor %}
</table>

<div id="confirm-deletion" class="modal hide fade">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title" id="myModalLabel">Delete collection</h4>
  </div>
  <div class="modal-body">
    <p>
      Be careful! You are about to delete whole <strong><span id="modal-collection-name"></span></strong> collection.
    </p>
    <p>
      <label for="confirmation-input">Type the collection name to proceed.</label>
      <input type="text" id="confirmation-input" name="confirmation-input" shouldbe="" value="" />
    </p>
  </div>
  
  <div class="modal-footer">
    <button type="button" data-dismiss="modal" class="btn" id="delete">Delete</button>
    <button type="button" data-dismiss="modal" class="btn btn-primary">Cancel</button>
  </div>
</div>

{% endblock %}
